package com.lesson01_JavaBase.str;

/**
 * 测试String,StringBuffer效率
 * StringBuffer和StringBuilder区别:
 * StringBuffer是线程安全的，StringBuilder是非线程安全
 *
 *
 * @author Administrator
 *
 */
public class TestStringBuffer {
    public static void main(String[] args) {
        testString();
        testStringBuffer();
    }

    // 字符串拼接比较多情况下效率非常高
    private static void testStringBuffer() { // 4
        StringBuffer sb = new StringBuffer("AAA");
        // 记录当前开始的时间
        long startTime = System.currentTimeMillis();
        for (int i = 1 ; i <=50000; i++) {
            sb.append("BBB");
        }
        long endTime = System.currentTimeMillis();
        System.out.println("StringBuffer用时:"+ (endTime - startTime));
    }


    private static void testString() { // 3292
        String str = "AAA";
        // 记录当前开始的时间
        long startTime = System.currentTimeMillis();
        for (int i = 1 ; i <=50000; i++) {
            str+="BBB";
            //System.out.println(str);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("String用时:"+ (endTime - startTime));

    }
}
